//
//  HMacro.swift
//  poetry
//
//  Created by YL-Hong on 2022/3/2.
//

import Foundation
import UIKit
import SnapKit


let kScreenWidth = UIScreen.main.bounds.size.width
let kScreenHeight = UIScreen.main.bounds.size.height

//判断是否是iphoneX
var IsIphoneX = isIPhoneXSeries() ? true : false
//nav高度
var KsafeAreaNaviBarHeight : CGFloat = isIPhoneXSeries()  ? 88.0 : 64.0
//底部安全区距离
var KsafeAreaBoHttomHeight : CGFloat = isIPhoneXSeries() ? 34.0 : 0.0
//tabbar 高度
var KsafeAreaTabBarHeight : CGFloat = isIPhoneXSeries() ? 84.0 : 50.0

/// 按屏幕宽度比例缩放
func scaleWidth(_ width:CGFloat) -> CGFloat {
   let scale = kScreenWidth/375
    
    return width * scale
}

/// 按屏幕高度比例缩放
func scaleHeight(_ height: CGFloat, _ screen: Bool = false) -> CGFloat {
    
    var scale = 0.0
    
    if screen == true{
        if kScreenHeight < 736{
            scale = kScreenWidth/375
        }else{
            scale = kScreenHeight/812
        }
    }else{
        scale = kScreenHeight/812
    }

    return height * scale
}

/// 获取KeyWindow
/// - Returns: 返回KeyWindow
func getKeyWindow() -> UIWindow? {
    var keyWindow: UIWindow?
    
    if #available(iOS 13, *){
        var windowScene: UIWindowScene?
        
        for item in UIApplication.shared.connectedScenes {
            if let temp = item as? UIWindowScene{
                windowScene = temp
            }
        }
        
        guard let wScene = windowScene else { return keyWindow}
        
        for item in wScene.windows {
            if item.isKeyWindow{
                keyWindow = item
            }
        }
    }else{
        keyWindow = UIApplication.shared.keyWindow
    }
    
    return keyWindow
}


/**无参 无返回值 Block*/
typealias ActionBlock = ()->Void

/**
 *鉴于iPhone X/XS/XR/XS Max底部都会有安全距离，所以可以利用safeAreaInsets.bottom > 0.0来判断是否是iPhone X/XS/XR/XSMax。
 */
private func isIPhoneXSeries() -> Bool {
    var  IPhoneXSeries:Bool = false
    
    if UIDevice.current.userInterfaceIdiom != UIUserInterfaceIdiom.phone {
        return IPhoneXSeries
    }
    if #available(iOS 11.0, *) {
        
        guard let kwindow = UIApplication.shared.delegate?.window else {
            return IPhoneXSeries
        }
        let Botton = kwindow.unsafelyUnwrapped.safeAreaInsets.bottom
        let NewBotton = Int (Botton)
        print(NewBotton)
        if  NewBotton > 0{
            IPhoneXSeries = true
        }
    }
    return IPhoneXSeries
}

///是否模拟器
func isSimuLator() -> Bool {
    if TARGET_IPHONE_SIMULATOR == 1 && TARGET_OS_IPHONE == 1 {
        //模拟器
        return true
    } else {
        //真机
        return false
    }
}

//通用日期格式对象
let CommonFormatter = DateFormatter()

// MARK:- 自定义打印方法
func YLLog<T>(_ message : T, file : String = #file, funcName : String = #function, lineNum : Int = #line) {
    
    #if DEBUG
        // 创建一个日期格式器
        let formatter = CommonFormatter
        // 为日期格式器设置格式字符串
        formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
        formatter.locale = Locale.init(identifier: "zh_Hans_CN")
        formatter.calendar = Calendar.init(identifier: .iso8601)
        // 使用日期格式器格式化当前日期、时间
        let datestr = formatter.string(from: Date())
    
        let fileName = (file as NSString).lastPathComponent
        
        print("[\(datestr)] - [\(fileName)] [第\(lineNum)行] \(message)")
        
    #endif
}

